import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xm_app/common/logger/LoggerUtil.dart';
import 'package:xm_app/common/ui/AppTheme.dart';
import 'package:xm_app/common/ui/ui_adapter.dart';
import 'package:xm_app/common/ui/ui_image.dart';
import 'package:xm_app/page/search/SearchController.dart';
import 'package:xm_app/routes/AppRoutes.dart';
import 'package:xm_app/widget/CommonGrid.dart';
import 'package:xm_app/widget/CommonText.dart';
import 'package:xm_app/widget/custom_refresh_widget.dart';
import 'package:xm_app/widget/input/DelInput.dart';

class SearchPage extends GetView<SearchController> {
  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: SearchController(),
      builder: (_) {
        return Scaffold(
          appBar: AppBar(
              backgroundColor: AppTheme.pinkColor,
              automaticallyImplyLeading: false,
              title: Row(
                children: [
                  Expanded(
                    child: Container(
                      constraints: BoxConstraints(maxHeight: getHeight(30)),
                      child: TextField(
                        controller: controller.inputController,
                        autofocus: false,
                        focusNode: controller.focusNode,
                        cursorColor: Colors.black,
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 14,
                        ),
                        textInputAction: TextInputAction.search,
                        onEditingComplete: () => controller.search(),
                        decoration: InputDecoration(
                          contentPadding: EdgeInsets.symmetric(vertical: getHeight(6)),
                          fillColor: Colors.white,
                          filled: true,
                          isDense: true,
                          border: OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(50)), borderSide: BorderSide.none),
                          hintText: "海量影片等你来搜索",
                          prefixIcon: Icon(
                            Icons.search,
                            color: Colors.grey,
                          ),
                          hintStyle: TextStyle(
                            color: Colors.grey,
                            fontSize: 14,
                            textBaseline: TextBaseline.alphabetic,
                          ),
                          suffixIcon: controller.inputController.text.length > 0
                              ? GestureDetector(
                                  onTap: () => controller.cleanText(),
                                  child: Icon(
                                    Icons.cancel,
                                    color: Color(0xFFcccccc),
                                  ),
                                )
                              : Text(""),
                        ),
                      ),
                    ),
                  ),
                  // Spacer(),
                  GestureDetector(
                    onTap: () => Get.back(),
                    child: Padding(
                      padding: EdgeInsets.symmetric(horizontal: 10),
                      child: CommonText(
                        "取消",
                        txtColor: Colors.white,
                        txtSize: getSp(14),
                      ),
                    ),
                  )
                ],
              )),
          body: Container(
            padding: EdgeInsets.symmetric(horizontal: 5),
            color: Colors.grey.withOpacity(.05),
            child: IndexedStack(
              index: controller.showIndex,
              children: [
                _buildDefault(),
                _buildSearch(),
              ],
            ),
          ),
        );
      },
    );
  }

  Widget _buildDefault() {
    return Container(
      child: Text('搜素记录'),
    );
  }

  Widget _buildSearch() {
    return CustomFooterLoadMoreWidget(
      onLoadMore: () => controller.loadData(),
      easyRefreshController: controller.easyRefreshController,
      slivers: [
        SliverToBoxAdapter(
          child: Column(
            children: [
              Padding(
                padding: EdgeInsets.symmetric(vertical: 10),
                child: Text.rich(TextSpan(
                  text: "搜索到与",
                  style: TextStyle(fontSize: getSp(14), letterSpacing: 1),
                  children: [
                    TextSpan(text: "\"${controller.keyword}\"", style: TextStyle(fontSize: getSp(14), color: AppTheme.iconYellow)),
                    TextSpan(text: "相关的", style: TextStyle(fontSize: getSp(14))),
                    TextSpan(text: "${controller.searchData.qty ?? 0}", style: TextStyle(fontSize: getSp(14), color: AppTheme.iconYellow)),
                    TextSpan(text: "条结果", style: TextStyle(fontSize: getSp(14))),
                  ],
                )),
              ),
              Divider(height: 1),
            ],
          ),
        ),
        SliverList(
          delegate: SliverChildBuilderDelegate(
            (BuildContext context, int index) {
              final item = controller.searchData.data[index];
              return Column(
                children: [
                  Container(
                    height: getHeight(150),
                    child: Card(
                      child: InkWell(
                        onTap: () {
                          toVodDetailPage(id: item.vodID);
                        },
                        child: Ink(
                          child: Row(
                            children: [
                              Padding(
                                padding: EdgeInsets.only(right: 10, top: 5, left: 5, bottom: 5),
                                child: ClipRRect(
                                  borderRadius: BorderRadius.circular(4),
                                  child: AspectRatio(
                                    aspectRatio: 110 / 150,
                                    child: Stack(
                                      children: [
                                        buildItemImage(item.vodPic),
                                        buildUpdateText(item.vodRemarks),
                                      ],
                                    ),
                                  ),
                                ),
                              ),
                              Expanded(
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    SizedBox(height: getHeight(15)),
                                    Container(
                                      child: CommonText(
                                        item.vodName,
                                        txtSize: getSp(16),
                                      ),
                                    ),
                                    SizedBox(height: getHeight(10)),
                                    Expanded(
                                      child: Column(
                                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                                        children: [
                                          Row(
                                            children: [
                                              CommonText(
                                                "主演：",
                                                txtColor: AppTheme.defaultTxtGrey,
                                              ),
                                              Expanded(
                                                child: CommonText.singleLine(
                                                  item.vodActor,
                                                  txtColor: AppTheme.defaultTxtGrey,
                                                ),
                                              )
                                            ],
                                          ),
                                          Row(
                                            children: [
                                              CommonText(
                                                "导演：",
                                                txtColor: AppTheme.defaultTxtGrey,
                                              ),
                                              Expanded(
                                                child: CommonText.singleLine(
                                                  item.vodDirector,
                                                  txtColor: AppTheme.defaultTxtGrey,
                                                ),
                                              )
                                            ],
                                          )
                                        ],
                                      ),
                                    ),
                                    SizedBox(height: getHeight(10)),
                                    Row(
                                      children: [
                                        CommonText(
                                          "查看详情",
                                          txtColor: AppTheme.iconYellow,
                                        ),
                                        Icon(
                                          Icons.arrow_forward_ios,
                                          color: AppTheme.iconYellow,
                                          size: getSp(15),
                                        )
                                      ],
                                    ),
                                    SizedBox(height: getHeight(10))
                                  ],
                                ),
                              )
                            ],
                          ),
                        ),
                      ),
                    ),
                  )
                ],
              );
            },
            childCount: controller.searchData?.data?.length ?? 0,
          ),
        )
      ],
    );
  }
}
